package stglxt;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class stgl {
    private static final String FILE_PATH = "D:\\JAVAA\\java1.txt";
    private static final String HUODONG_FILE_PATH = "D:\\JAVAA\\java2.txt";

    // 从文件中读取成员信息并返回成员列表
    public static List<stcy> duchengyuanxx() {
        List<stcy> members = new ArrayList<>();
        File file = new File(FILE_PATH);
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String dwj;
            while ((dwj = reader.readLine())!= null) {
                String[] parts = dwj.split(",");
                if (parts.length == 3) {
                    members.add(new stcy(parts[0], parts[1], parts[2]));
                }
            }
        } catch (IOException e) {
            System.out.println("读取成员文件出现错误: " + e.getMessage());
        }
        return members;
    }

    // 将成员列表信息写入文件
    public static void xxxierutxt(List<stcy> members) {
        File file = new File(FILE_PATH);
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            for (stcy member : members) {
                writer.write(member.getMz() + "," + member.getXsId() + "," + member.getStmz() + "\n");
            }
        } catch (IOException e) {
            System.out.println("写入成员文件出现错误: " + e.getMessage());
        }
    }

    // 从活动记录文件读取活动信息
    // 从活动记录文件读取活动信息
    public static List<shetuanhuodong> readActivityRecords() {
        List<shetuanhuodong> activities = new ArrayList<>();
        File file = new File(HUODONG_FILE_PATH);
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String hang;
            while ((hang = reader.readLine())!= null) {
                String[] hangparts = hang.split(",");
                shetuanhuodong activity = new shetuanhuodong(hangparts[0], hangparts[1], hangparts[2]);  // 构建对象时传入活动内容
                for (int i = 3; i < hangparts.length; i += 3) {
                    for (stcy member : duchengyuanxx()) {
                        if (member.getXsId().equals(hangparts[i])) {
                            activity.addParticipant(member);
                            break;
                        }
                    }
                }
                activities.add(activity);
            }
        } catch (IOException e) {
            System.out.println("读取活动记录文件出现错误: " + e.getMessage());
        }
        return activities;
    }

    // 将活动记录写入文件
    // 将活动记录写入文件
    public static void writeActivityRecords(List<shetuanhuodong> activities) {
        File file = new File(HUODONG_FILE_PATH);
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            for (shetuanhuodong activity : activities) {
                writer.write(activity.getHdmz() + "," + activity.getActivityDate() + "," + activity.getHdneirong());  // 写入活动内容
                for (stcy member : activity.getCanjiacy()) {
                    writer.write("," + member.getXsId() + "," + member.getMz() + "," + member.getStmz());
                }
                writer.write("\n");
            }
        } catch (IOException e) {
            System.out.println("写入活动记录文件出现错误: " + e.getMessage());
        }
    }

    public static void tianjiachengyuan(List<stcy> members) {
        Scanner shuru = new Scanner(System.in);
        System.out.println("请输入要添加的成员姓名:");
        String name = shuru.nextLine();
        System.out.println("请输入要添加的成员学号:");
        String studentId = shuru.nextLine();
        System.out.println("请输入该成员选择的社团名称:");
        String clubName = shuru.nextLine();
        members.add(new stcy(name, studentId, clubName));
        xxxierutxt(members);
        System.out.println("成员添加成功!");
    }

    public static void sccy(List<stcy> members) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的成员学号:");
        String studentIdToDelete = sc.nextLine();
        for (int i = 0; i < members.size(); i++) {
            if (members.get(i).getXsId().equals(studentIdToDelete)) {
                members.remove(i);
                xxxierutxt(members);
                System.out.println("成员删除成功!");
                return;
            }
        }
        System.out.println("未找到对应学号的成员，删除失败!");
    }

    public static void zschengyuanxx(List<stcy> members) {
        System.out.println("社团成员列表:");
        for (stcy member : members) {
            System.out.println("姓名: " + member.getMz() + ", 学号: " + member.getXsId() + ", 社团: " + member.getStmz());
        }
    }

    // 记录社团活动开展
    // 记录社团活动开展
    public static void jilushetuanhd(List<stcy> members) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入活动名称:");
        String activityName = scanner.nextLine();
        System.out.println("请输入活动日期:");
        String activityDate = scanner.nextLine();
        System.out.println("请输入活动内容:");  // 添加输入活动内容提示
        String activityContent = scanner.nextLine();
        shetuanhuodong activity = new shetuanhuodong(activityName, activityDate, activityContent);
        System.out.println("请输入参与活动的成员学号（输入'q'结束）:");
        String input;
        do {
            input = scanner.nextLine();
            if (!input.equals("q")) {
                for (stcy member : members) {
                    if (member.getXsId().equals(input)) {
                        activity.addParticipant(member);
                        break;
                    }
                }
            }
        } while (!input.equals("q"));
        List<shetuanhuodong> activities = readActivityRecords();
        activities.add(activity);
        writeActivityRecords(activities);
        System.out.println("社团活动记录成功!");
    }

    // 导出社团活动开展记录
    // 导出社团活动开展记录
    public static void daochushetuanhuodong() {
        List<shetuanhuodong> activities = readActivityRecords();
        System.out.println("活动记录：");
        for (shetuanhuodong activity : activities) {
            System.out.println("活动名称: " + activity.getHdmz() + ", 活动日期: " + activity.getActivityDate());
            System.out.println("活动内容: " + activity.getHdneirong());  // 输出活动内容
            System.out.println("参与成员:");
            for (stcy member : activity.getCanjiacy()) {
                System.out.println("    姓名: " + member.getMz() + ", 学号: " + member.getXsId() + ", 社团: " + member.getStmz());
            }
        }
    }

    public static void main(String[] args) {
        List<stcy> members = duchengyuanxx();
        Scanner scanner = new Scanner(System.in);
        boolean exit = false;
        while (!exit) {
            System.out.println("请选择操作: 1.添加成员 2.删除成员 3.查看成员列表 4.记录社团活动 5.导出社团活动记录 6.退出");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    tianjiachengyuan(members);
                    break;
                case 2:
                    sccy(members);
                    break;
                case 3:
                    zschengyuanxx(members);
                    break;
                case 4:
                    jilushetuanhd(members);
                    break;
                case 5:
                    daochushetuanhuodong();
                    break;
                case 6:
                    exit = true;
                    break;
                default:
                    System.out.println("无效的选择，请重新输入!");
            }
        }
    }
}
